
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.twitter.acount;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tompai.common.time.TimeTransUtils;
import com.tompai.datagather.common.MybatisUtil;
import com.tompai.datagather.dao.TwitterAcountRelationshipMapper;
import com.tompai.datagather.dao.TwitterAcountSeedsMapper;
import com.tompai.datagather.model.TwitterAcountRelationship;
import com.tompai.datagather.model.TwitterAcountSeeds;
import com.tompai.datagather.model.TwitterAcounts;
import com.tompai.datagather.twitter.InvalidQueryException;
import com.tompai.datagather.utils.TwitterParamater;

/**
* @desc: datagather-tools
* @name: TwitterFollowBatch.java
* @author: tompai
* @createTime: 2018年5月17日 下午2:49:26
* @history:
* @version: v1.0
*/

public class TwitterFollowBatch extends TwitterFollowSearch {

	public static final Logger LOG = LoggerFactory.getLogger(TwitterFollowPage.class);

	private static String twitterHome = "https://twitter.com/";
	private final AtomicInteger counter = new AtomicInteger();

	@Override
	public boolean saveTacounts(String acount, String role, List<TwitterAcounts> tweetBeans) {
		// TODO Auto-generated method stub
		//int index = 0;
		// a count the max relationship//
		int maxlimit = TwitterParamater.TWITTER_FOLLOW_LIMIT;
		
		List<TwitterAcountSeeds> seedlists = new ArrayList<>();
		TwitterAcountSeeds seed;
		
		List<TwitterAcountRelationship> relationlists = new ArrayList<>();
		TwitterAcountRelationship relationship;
		
		TwitterAcountSeedsMapper seedsMapper;
		seedsMapper = MybatisUtil.getSession().getMapper(TwitterAcountSeedsMapper.class);

		TwitterAcountRelationshipMapper relationshipMapper;
		relationshipMapper = MybatisUtil.getSession().getMapper(TwitterAcountRelationshipMapper.class);

		if (tweetBeans != null) {
			for (TwitterAcounts tweet : tweetBeans) {
				LocalDateTime datetime = LocalDateTime.now();
				String screenName = tweet.getProfileScreenname();
				if (counter.get() >= maxlimit) {
					counter.set(0);
					LOG.info("The Follow have beyond the limit:" + maxlimit);
					return false;
				}
				seed = new TwitterAcountSeeds();
				seed.setAcount(screenName);
				seed.setHomepage(twitterHome + screenName);
				seed.setInputTime(TimeTransUtils.localDateTimeToDate(datetime));
				seedlists.add(seed);
				
				relationship = new TwitterAcountRelationship();
				relationship.setAccounta(acount);
				relationship.setAccountb(screenName);
				relationship.setRole(role);
				relationship.setInputTime(TimeTransUtils.localDateTimeToDate(datetime));
				relationlists.add(relationship);
				LOG.info(counter.getAndIncrement() + 1 + "[" + acount + "--" + role + "--" + screenName + "] ");
			}
			//批量入库方式//
			seedsMapper.insertBatch(seedlists);
			relationshipMapper.insertBatch(relationlists);
			MybatisUtil.getSession().commit();
			LOG.info("Batch input finished!");
		}
		return true;
	}

	/**
	 * @author: tompai
	 * @createTime: 2018年5月8日 下午11:13:46
	 * @history:
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {

		String[] acounts = { "shamlegion10", "amjadmedia9", "TheNationalUAE", "narendramodi" };
		// String acount = "ShamLegion";
		String roleA = "following";//
		String roleB = "followers";
		int rateDelay = 100;
		try {
			TwitterFollowSearch followSearch = new TwitterFollowBatch();
			for (String acount : acounts) {
				followSearch.searchFollow(acount, roleA, rateDelay);
				followSearch.searchFollow(acount, roleB, rateDelay);
			}
		} catch (InvalidQueryException e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void parseFollow(String acount, String role) {

		// String acount = "ShamLegion";
		// String role = "following";// followers
		int rateDelay = 100;
		try {
			TwitterFollowSearch followSearch = new TwitterFollowPage();
			followSearch.searchFollow(acount, role, rateDelay);
		} catch (InvalidQueryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

